﻿// $Id: CompositeQuestion.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.UI.CompositeQuestion.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Salamanca.DataActivities.UI
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Question which acts as the composition of elementary <see cref="IQuestion" />s.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class CompositeQuestion:
        Question
    {

        /// <summary>Initializes a new instance of the <see cref="CompositeQuestion" /> class.</summary>
        public CompositeQuestion(IDictionary<string, IQuestion> innerQuestions)
        {
            _InnerQuestions=innerQuestions;
        }

        /// <summary>Asks the composite question.</summary>
        public override ActivityStateResult Ask(IActivityController controller)
        {
            ActivityStateResult ret=ActivityStateResult.Stop;

            foreach (KeyValuePair<string, IQuestion> kvp in _InnerQuestions)
            {
                IActivityController c=new CompositeController(((ICompositeActivityState)controller.CurrentState).InnerStates[kvp.Key]);
                kvp.Value.Ask(c);
            }

            return ret;
        }

        /// <summary>Raises the <see cref="Question.Initialized" /> event.</summary>
        protected override void OnInitialized(QuestionEventArgs e)
        {
            ICompositeActivityState cs=(ICompositeActivityState)e.Controller.CurrentState;

            foreach (KeyValuePair<string, IQuestionActivityState> kvp in cs.InnerStates)
            {
                if (kvp.Value.Question!=null)
                {
                    kvp.Value.Question.Terminate(e.Controller);
                    kvp.Value.Question=null;
                }
            }

            foreach (KeyValuePair<string, IQuestion> kvp in InnerQuestions)
            {
                if (cs.InnerStates.ContainsKey(kvp.Key))
                {
                    cs.InnerStates[kvp.Key].Question=kvp.Value;
                    kvp.Value.Initialize(new CompositeController(cs.InnerStates[kvp.Key]));
                }
            }

            base.OnInitialized(e);
        }

        /// <summary>Raises the <see cref="Question.Terminated" /> event.</summary>
        protected override void OnTerminated(QuestionEventArgs e)
        {
            ICompositeActivityState cs=(ICompositeActivityState)e.Controller.CurrentState;

            foreach (KeyValuePair<string, IQuestionActivityState> kvp in cs.InnerStates)
            {
                if (kvp.Value.Question!=null)
                {
                    kvp.Value.Question.Terminate(e.Controller);
                    kvp.Value.Question=null;
                }
            }

            base.OnTerminated(e);
        }

        /// <summary>Gets the questions that compose the current <see cref="CompositeQuestion" />.</summary>
        public IDictionary<string, IQuestion> InnerQuestions
        {
            get
            {
                return _InnerQuestions;
            }
        }

        /// <summary>The questions that compose the current <see cref="CompositeQuestion" />.</summary>
        private IDictionary<string, IQuestion> _InnerQuestions;
    }
}
